package com.iraeta.uitl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.*;

import com.alibaba.fastjson.JSONArray;

public class Util {
    public static String parseListToString(List<? extends Object> list) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if (i > 0) {
                sb.append(",");
            }
            sb.append(list.get(i).toString());
        }
        return sb.toString();
    }
    public static String parseListToSqlString(List<? extends Object> list) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if (i > 0) {
                sb.append(",");
            }
            sb.append("'").append(list.get(i).toString()).append("'");
        }
        return sb.toString();
    }
    public static String parseIntsToString(Object... paras) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < paras.length; i++) {
            if (i > 0) {
                sb.append(",");
            }
            sb.append(paras[i].toString());
        }
        return sb.toString();
    }
    public static String parseJSONArrayToString(JSONArray ids) {
        if(ids.size() == 0){
            return "";
        }
        boolean first = true;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ids.size(); i++) {
            if (!first) {
                sb.append(",");
            }
            sb.append(ids.getString(i));
            first = false;
        }
        return sb.toString();
    }
    public static JSONArray parseStringToJSONArray(String splitStr) {
        String[] ids = splitStr.split(",");
        JSONArray jsonArray = new JSONArray();
        int i = 0;
        for(String idStr : ids){
            jsonArray.add(i, Integer.parseInt(idStr));
            i++;
        }
        return jsonArray;
    }
    public static int parseIntFromMap(Map<String, Object> row, String key) {
        if(!row.containsKey(key)){
            return 0;
        }
        String val = row.get(key).toString();
        if(StrUtil.isEmpty(val)){
            return 0;
        }
        return Integer.parseInt(val);
    }
    public static String parseStringFromMap(Map<String, Object> row, String key) {
        if (!row.containsKey(key))
            return null;
        Object o = row.get(key);
        if (o == null) {
            return null;
        }
        return o.toString();
    }
    public static String toStackTrace(Exception e){
        StringWriter sw  = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try {
            e.printStackTrace(pw);
            return sw.toString();
        }catch (Exception e1){
            return "未知错误";
        }
    }

    public static String getBusinessNo() {
        StringBuffer sb = new StringBuffer();
        String dateStr = DateUtil.format(new Date(),"YYYYMMddHHmmss");
        int random = new Random().nextInt(9000) + 1000;
        sb.append(dateStr).append(String.valueOf(random));
        return sb.toString();
    }

    public static int getRandomInt(){
        int a = - new Random().nextInt(1000000);
        return a ;
    }

    public static String getName(String fileName) {
        if (fileName == null)
        {
            return null;
        }
        int lastUnixPos = fileName.lastIndexOf('/');
        int lastWindowsPos = fileName.lastIndexOf('\\');
        int index = Math.max(lastUnixPos, lastWindowsPos);
        return fileName.substring(index + 1);
    }

    public static void mapToBean(HashMap<String, Object> map, Object item){
        Method[] methods = item.getClass().getMethods();
        for (String k : map.keySet()) {
            String lk = k.toLowerCase();
            Optional<Method> opMethod = Arrays.stream(methods).filter(s -> s.getName().toLowerCase().equals("set" + lk)).findFirst();
            if (!opMethod.isPresent())
                continue;

            Method method = opMethod.get();
            Object mapValue = map.get(k);
            if(mapValue != null){
                String mapStringValue = mapValue.toString();
                try {
                    Object targetValue;
                    String fieldType = method.getParameters()[0].getType().toString();
                    switch (fieldType) {
                        case "boolean":
                        case "class java.lang.Boolean":
                            if (mapStringValue.equals("1") || mapStringValue.equals("true")) {
                                targetValue = true;
                            } else {
                                targetValue = false;
                            }
                            break;
                        case "int":
                        case "class java.lang.Integer":
                            if (StrUtil.isEmpty(mapStringValue)) {
                                targetValue = 0;
                            } else {
                                targetValue = Integer.parseInt(mapStringValue);
                            }
                            break;
                        case "long":
                        case "class java.lang.Long":
                            if (StrUtil.isEmpty(mapStringValue)) {
                                targetValue = 0;
                            } else {
                                targetValue = Long.parseLong(mapStringValue);
                            }
                            break;
                        case "float":
                        case "class java.lang.Float":
                            if (StrUtil.isEmpty(mapStringValue)) {
                                targetValue = 0;
                            } else {
                                targetValue = Float.parseFloat(mapStringValue);
                            }
                            break;
                        case "double":
                        case "class java.lang.Double":
                            if (StrUtil.isEmpty(mapStringValue)) {
                                targetValue = 0;
                            } else {
                                targetValue = Double.parseDouble(mapStringValue);
                            }
                            break;
                        case "class java.math.BigDecimal":
                            if (StrUtil.isEmpty(mapStringValue)) {
                                targetValue = 0;
                            } else {
                                targetValue = new BigDecimal(mapStringValue);
                            }
                            break;
                        case "class java.util.Date":
                            if (StrUtil.isEmpty(mapStringValue)) {
                                targetValue = null;
                            } else {
                                //targetValue = new Date(DateUtil.parseDate(mapStringValue).toInstant().toEpochMilli());
                                targetValue = DateUtil.parseDate(mapStringValue);
                            }
                            break;
                        default:
                            if (StrUtil.isEmpty(mapStringValue)) {
                                targetValue = null;
                            } else {
                                targetValue = mapStringValue;
                            }
                            break;
                    }
                    method.invoke(item, targetValue);
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        }
    }
}
